/**
  ETFDAQ Project
  \class TAv895
  \brief 16 ch ECL input LED VME module from CAEN Corp. of Italy
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2024-09-03
  \date 2024-09-03 last modified

  \copyright Copyright (c) 2024 IMP-CAS with LGPLv3 LICENSE
*/

#ifndef TAv895_h
#define TAv895_h

#include "TACAENCard.h"

class TAv895 : public TACAENCard{
public:
  TAv895(TAController *ctrl, const TAYaml &c);
  virtual ~TAv895();

  ///--- initialization ---///
  virtual void Initialize() override;
  virtual void Identify() override; ///< read card info (e.g. serialNo.)
  virtual void SlotInit() override{} ///< write slot id and crate id
  virtual void SetThreshold(int ch, uint16_t thre) const override;
  /// \param first: 0-7; false: 8-15; \param w: 0: 5ns; 255: 40ns
  virtual void SetOutputWidth(bool first, int w) const;

  ///--- configuration ---///
  /// reset counter regs, the MEB, the trig counter, and the MEB ev counter
  virtual void SoftwareReset() override{}

  ///--- the DAQ loop ---///
  virtual bool DataReady() const override{ return false; } ///< just LED, no data
  virtual int ReadEvent(uint32_t *buf) override{ return 0; }
  virtual void ClearData() override{}
  virtual void ClearEventCounter() override{}

  /// ---- print functions ---///
  virtual void Print() const override;

  virtual void SetChOnPattern(uint16_t pattern) const;

protected:
  int fModuleType, fManufacturerNo;
  int fWidth[2]; // [0-7, 8-15], in ns
};


// control and data registers specifically for v895 //
struct v895reg{
  static constexpr uint16_t Thresholds =          0x00;
  static constexpr uint16_t Width0_7 =            0x40; // 0: 5ns
  static constexpr uint16_t Width8_15 =           0x42; // 255: 40ns
  static constexpr uint16_t MajorityThre =        0x48;
  static constexpr uint16_t PatternInhibit =      0x4a; // channel enable
  static constexpr uint16_t TestPulse =           0x4c;
  static constexpr uint16_t FixedCode =           0xfa;
  static constexpr uint16_t ModuleType =          0xfc;
  static constexpr uint16_t SerialNo =            0xfe;
};

#endif
